/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skat.collections;

import java.util.ArrayList;
import java.util.Collections;

import skat.Card;
import skat.decks.Deck;

/**
 * 
 * @author weinpau
 */
public class CardStack extends ArrayList<Card> {
	private Deck deck;
	private boolean shuffeled;

	public CardStack(Deck d) {
		setDeck(d);
		reset();
		shuffle();
	}

	public void reset() {
		clear();
		for (int c = 0; c < deck.suitCount(); c++) {
			for (int i = 0; i < deck.rankCount(); i++) {
				add(new Card(c, i));
			}
		}
		shuffeled = false;
	}

	public Hand getHand() { // FIXME: nach Game verschieben
		return getHand(10);
	}

	public Hand getTalon() { // FIXME: nach Game verschieben
		return getHand(2);
	}

	public void shuffle() {
		if (!shuffeled) {
			Collections.shuffle(this);
			shuffeled = true;
		}
	}

	private Hand getHand(int n) {
		Hand hand = new Hand();
		hand.addAll(draw(n));
		return hand;
	}

	public Card draw() {
		Card c = get(0);
		remove(0);
		return c;
	}

	public ArrayList<Card> draw(int n) {
		ArrayList<Card> al = new ArrayList<Card>();
		for (int i = 0; i < n; i++) {
			al.add(draw());
		}
		return al;
	}

	public Card top() {
		return get(0);
	}

	public void setDeck(Deck deck) {
		this.deck = deck;
	}

}
